package net.nandleon.AgenceVoyage.server;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUpload extends HttpServlet {
	 
	     private static final String UPLOAD_DIRECTORY = "C:\\Users\\Gaetan\\";
	 
	     @Override
	     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	             throws ServletException, IOException {
	         super.doGet(req, resp);
	     }
	 
	     @Override
	     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	             throws ServletException, IOException {
	    	 try {
	    	      ServletFileUpload upload = new ServletFileUpload();
	    	      resp.setContentType("text/plain");

	    	      FileItemIterator iterator = upload.getItemIterator(req);
	    	      while (iterator.hasNext()) {
	    	        FileItemStream item = iterator.next();
	    	        InputStream stream = item.openStream();
	    	          // You now have the filename (item.getName() and the
	    	          // contents (which you can read from stream). Here we just
	    	          // print them back out to the servlet output stream, but you
	    	          // will probably want to do something more interesting (for
	    	          // example, wrap them in a Blob and commit them to the
	    	          // datastore).
	    	          int len;
	    	          byte[] buffer = new byte[8192];
	    	          while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
	    	            resp.getOutputStream().write(buffer, 0, len);
	    	          }
	    	
	    	        }
	    	    
	    	    } catch (Exception ex) {
	    	      throw new ServletException(ex);
	    	    }

	     }
	 }